using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace TestFileApi
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("测试AddFile接口...");
            
            // 创建测试数据，模拟MauiApp发送的请求
            var testData = new
            {
                FileName = "test_file.txt",
                SavedPath = "C:\\temp\\test_file.txt",
                FileTypeId = 1,  // 假设文件类型ID为1
                // 不设置UserId字段，因为这个字段在服务器端是可选的
                FtpPath = "/ftp/files/test_file.txt",
                Time = DateTime.Now,
                OpenTime = DateTime.Now
            };
            
            // 序列化测试数据
            var jsonContent = new StringContent(
                JsonSerializer.Serialize(testData),
                Encoding.UTF8,
                "application/json");
            
            // 发送请求
            using var httpClient = new HttpClient();
            var url = "http://localhost:5269/api/file/add";
            
            try
            {
                Console.WriteLine($"向 {url} 发送请求...");
                var response = await httpClient.PostAsync(url, jsonContent);
                
                // 输出响应状态
                Console.WriteLine($"响应状态码: {response.StatusCode}");
                Console.WriteLine($"响应内容: {await response.Content.ReadAsStringAsync()}");
                
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("测试成功！AddFile接口正常工作。");
                }
                else
                {
                    Console.WriteLine("测试失败！AddFile接口返回错误。");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"请求过程中发生错误: {ex.Message}");
            }
            
            Console.WriteLine("测试完成，请按任意键退出...");
            Console.ReadKey();
        }
    }
}